<template>
    <div class="shopcarContainer">
        <!-- 商品列表区域 -->
        <div class="goodsList">
            <div class="mui-card" v-for="(item,i) in goodsList" :key="item.id">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <mt-switch v-model="$store.getters.getgoodStatus[item.id]"
                        @change="statusChanged(item.id,$store.getters.getgoodStatus[item.id])"></mt-switch>
                        <img :src="item.img_url" alt="">
                        <div class="info">
                            <h1>{{item.title}}</h1>
                            <p>
                                <span class="price">${{item.sell_price}}</span>
                                <numbox :initcount="$store.getters.getgoodsCount[item.id]" :goodsid="item.id"></numbox>
                                <!-- 从购物车中获取商品数量 -->
                                <!-- 由于是个a链接，所以要用prevent -->
                                <a href="" @click.prevent="remove(item.id,i)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
       
        <!-- 结算区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="left">
                        <p>总计（不含运费）</p>
                        <p>已勾选商品<span class="red">{{ $store.getters.getcountAndamout.count}}</span>件，总价<span class="red">￥{{ $store.getters.getcountAndamout.amount}}</span></p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
               
            </div>
        </div>
         <!-- <div>{{ $store.getters.getgoodsCount }}</div> -->
    </div>
</template>
<script>
import numbox from '../subcomponents/shopCart_numbox.vue'
export default {
    data(){
        return{
            goodsList:[]
        }
    },
    components: {
        numbox
    },
    created(){
        this.getgoodsList()
    },
    methods:{
        getgoodsList(){
            //利用商品id将其挂在请求路径后面
            // var idArr = [];
            // this.$store.state.car.forEach(item => idArr.push(item.id));
            // if(idArr.length <= 0){
            //     return;
            // }
            this.$http.get("src/json/goodsList.json").then(result => {
                if(result.body.status === 0){
                    this.goodsList = result.body.message
                }
            })
        },
        remove(id,index){
            this.goodsList.splice(index,1);
            this.$store.commit("removeFromCar",id)
        },
        statusChanged(id,status){
            //点击开关，把最新的状态同步到store中
            console.log(id +"----"+ status)
            this.$store.commit("updateStatus",{id,selected:status});
        }
    }

}
</script>
<style lang="scss" scoped>
.shopcarContainer{
    background-color: #eee;
    overflow: hidden;
    .red{
        color:red;
        font-weight: bold;
    }
}
.goodsList{
    .mui-card-content-inner{
        display: flex;
        align-items: center;
    }
    img{
        width: 60px;
        height: 60px;
    }
    .info{
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        h1{
            font-size: 14px;
        }
        .price{
            color:red;
            font-weight: bold;
        }
    }

   
}
 .mui-card-content-inner{
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
</style>

